package com.ly.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.entity.Food;
import com.ly.service.FoodService;
import com.ly.utils.ResposeData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Tag(name = "菜品信息接口")
@CrossOrigin
@RestController
public class FoodController {

    @Autowired
    private FoodService foodService;

    @Value("${path1}")
    private String path;

    @Operation(summary = "分页查询菜品")
    @GetMapping("/queryAll")
    public ResposeData queryAll(Food food,@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5")  int pageSize){
        try {
            System.out.println(food.getTitle());
            System.out.println(food.getTypeid());
            PageHelper.startPage(pageNum,pageSize);
            List<Map> list = foodService.queryAll(food);
            PageInfo pageInfo = new PageInfo(list,3);
            return ResposeData.ok().put("list",pageInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "修改菜品")
    @PostMapping ("/updateFood")
    public ResposeData updateFood(Food food){
        System.out.println(food.getTitle());
        foodService.updateFood(food);
        return ResposeData.ok();
    }

    @Operation(summary = "删除菜品")
    @PostMapping("/deleteFood")
    public ResposeData daleteFood(Food food){
        foodService.deleteFood((int) food.getId());
        return ResposeData.ok();
    }

    @Operation(summary = "添加菜品")
    @PostMapping("/addFood")
    public ResposeData addFood(Food food, MultipartFile files){

        if(files!=null){
            try {
                //这个是获得原始文件名称
                String file1FileName = files.getOriginalFilename();
//使用当前时间毫秒数 做为文件名称
                file1FileName= System.currentTimeMillis()+"id"+file1FileName.substring(file1FileName.lastIndexOf("."),file1FileName.length());
                File dir=new File(path+"/images");
                if(!dir.exists()){  //如果这个文件夹不存在就创建一个
                    dir.mkdirs();
                }

                //保存文件的地址
                File file=new File(dir,file1FileName);
                //开始接收    获得读文件的流
                InputStream is = files.getInputStream();

                FileOutputStream fos=new FileOutputStream(file);
                byte [] b=new byte[1024*100];  //开辟一个空间 100KB
                int len=0; //len 的到实际接收的大小
                while(  (len=is.read(b))>0  ){
                    fos.write(b, 0, len);
                }
                fos.close();
                is.close();
                //成功为200
                //把图片的名称 放到对象里面
                food.setPic(file1FileName);
                //调用service添加
            } catch (Exception e) {
                e.printStackTrace();
                return ResposeData.currError ("文件上传错误");
            }
        }
//        if(emp.getHiredate()==null){//如果前端没有传时间,那么就给一个默认时间
//            emp.setHiredate(new Date());
//        }
        System.out.println("图片名称："+files);
        foodService.addFood(food);
        return ResposeData.ok().put("food",food);


    }
}
